package tq.com.tqstudent.ui.activity.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lib.common.base.BaseActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.OnClick;
import tq.com.tqstudent.App;
import tq.com.tqstudent.R;
import tq.com.tqstudent.net.ok.StringCallback;

/**
 * @Author rames
 * @date 2019/3/5 17:35
 */
public class FeedbackActivity extends BaseActivity {
    @BindView(R.id.content_edit)
    EditText contentEdit;
    @BindView(R.id.index_tv)
    TextView indexTV;

    public static void show(Activity activity) {
        Intent intent = new Intent(activity, FeedbackActivity.class);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    public int initLayout() {
        return R.layout.activity_feedback;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setTitle("意见反馈");
    }

    @Override
    public void initData() {
        contentEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                indexTV.setText(String.valueOf(contentEdit.getText().toString().length()));
            }
        });
    }

    @OnClick(R.id.commit_tv)
    public void onClick(View v){
        if(contentEdit.getText().toString().length() < 1){
            showToast("请输入反馈内容再提交!");
            return;
        }

        commitTask();
    }

    private void commitTask(){
        String content = contentEdit.getText().toString();
        String json = "{\"content\":\""+content+"\"}";

        OkGo.<String>post(App.getHttpUrl("api/user/feedback"))
                .tag(this)
                .upJson(json)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        showToast(response.message());
                        FeedbackActivity.this.finish();
                    }
                });
    }
}
